package com.yanqu.road.server.cmd.season;

import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.season.SeasonRewardKey;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SeasonRewardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.season.BaseSeasonFactory;
import com.yanqu.road.server.logic.season.BaseSeasonReward;
import com.yanqu.road.server.logic.season.args.ReceiveSeasonRewardResult;
import com.yanqu.road.server.pb.SeasonRewardPb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_SEASON_REWARD_GET_REWARD, desc = "赛季领奖")
public class GetSeasonRewardCmd extends AbstractCommand {

    private SeasonRewardKey key;
    private List<Integer> idList = new ArrayList<>();

    private String reward;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SeasonRewardProto.SeasonRewardGetRewardReqMsg reqMsg = SeasonRewardProto.SeasonRewardGetRewardReqMsg.parseFrom(packet.getBytes());
        key = SeasonRewardPb.parseSeasonRewardKey(reqMsg.getKey());
        idList = reqMsg.getRewardIdList();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 拿一下配置
        eSeasonRewardType enumType = eSeasonRewardType.getType(key.getType());
        if (enumType == null) {
            ret = GameErrorCode.E_SEASON_REWARD_TYPE_NO_EXIST;
            return;
        }
        BaseSeasonReward instance = BaseSeasonFactory.getInstance(enumType, player);

        // 领奖
        ReceiveSeasonRewardResult result = instance.receiveReward(key, idList);
        if (result.getRet() != 0) {
            ret = result.getRet();
            return;
        }
        reward = result.getReward();
    }

    @Override
    public void sendData(GamePlayer player) {
        SeasonRewardProto.SeasonRewardGetRewardRespMsg.Builder builder = SeasonRewardProto.SeasonRewardGetRewardRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setReward(reward);
        }
        player.sendPacket(ClientProtocol.U_SEASON_REWARD_GET_REWARD, builder);
    }
}
